import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { AppModule } from './app.module';
import * as ejs from 'ejs';
import cookieParser = require('cookie-parser');
import { UnauthorizedFilter } from './auth/unauthorized.filter';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  // 配置全局过滤器
  app.useGlobalFilters(new UnauthorizedFilter());
  
  // 配置cookie解析
  app.use(cookieParser());

  // 配置静态资源服务
  app.useStaticAssets(join(__dirname, '..', 'public'));

  // 配置 EJS 模板引擎
  app.setBaseViewsDir(join(__dirname, '..', 'views'));
  app.setViewEngine('html');
  app.engine('html', ejs.renderFile);
  
  // 配置全局验证管道
  app.useGlobalPipes(new ValidationPipe());

  await app.listen(3000);
}
bootstrap();